package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.domain.SysTeaStu;
import com.excellent.system.domain.SysTerm;
import com.excellent.system.service.ISysTaskStuService;
import com.excellent.system.service.ISysTeaStuService;
import com.excellent.system.service.ISysTermService;
import com.excellent.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

/**
 * 学期课题Controller
 *
 * @author excellent
 * @date 2021-06-18
 */
@Controller
@RequestMapping("/system/term")
public class SysTermController extends BaseController
{
    private String prefix = "system/term";

    @Autowired
    private ISysTermService sysTermService;

    @Autowired
    private ISysTeaStuService sysTeaStuService; //导入学生教师的全部的信息

    @Autowired
    private ISysUserService sysUserService; // 导入全部用户的信息

    @Autowired
    private ISysTaskStuService sysTaskStuService;//导入学生各个学期实践环节

    @GetMapping()
    public String term()
    {
        return prefix + "/term";
    }

    /**
     * 查询学期课题列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTerm sysTerm)
    {
        startPage();
        List<SysTerm> list = sysTermService.selectSysTermList(sysTerm);
        return getDataTable(list);
    }

    /**
     * 导出学期课题列表
     */
    @Log(title = "学期课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTerm sysTerm)
    {
        List<SysTerm> list = sysTermService.selectSysTermList(sysTerm);
        ExcelUtil<SysTerm> util = new ExcelUtil<SysTerm>(SysTerm.class);
        return util.exportExcel(list, "term");
    }

    /**
     * 新增学期课题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学期课题
     */
    @Log(title = "学期课题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTerm sysTerm)
    {
        return toAjax(sysTermService.insertSysTerm(sysTerm));
    }

    /**
     * 修改学期课题
     */
    @GetMapping("/edit/{termId}")
    public String edit(@PathVariable("termId") Long termId, ModelMap mmap)
    {
        SysTerm sysTerm = sysTermService.selectSysTermById(termId);
        mmap.put("sysTerm", sysTerm);
        return prefix + "/edit";
    }

    /**
     * 修改保存学期课题
     */
    @Log(title = "学期课题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTerm sysTerm)
    {
        return toAjax(sysTermService.updateSysTerm(sysTerm));
    }

    /**
     * 删除学期课题
     */
    @Log(title = "学期课题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTermService.deleteSysTermByIds(ids));
    }


    @PostMapping("/generate")
    @ResponseBody
    public AjaxResult genarate(){
        boolean check = false;  //当前学期是否已经生成
        SysTaskStu sysTask = new SysTaskStu();
        Calendar c1 = Calendar.getInstance();  //获取系统当前年份
        int m = c1.get(Calendar.MONTH) + 1;//本函数中月份需要加一
        int y = c1.get(Calendar.YEAR);
        if(m>=1 && m<=6){
            sysTask.setTaskTerm((y-1)+"-"+(y) + "学年春季学期");
        } else {
            sysTask.setTaskTerm((y)+"-"+(y + 1) + "学年秋季学期");
        }
        List<SysTaskStu> l = sysTaskStuService.selectSysTaskStuList(sysTask); //判断有没有当前学期的实践环节
        if(l.size()>0){
            check = false;
        }else {
            check = true;
        }
        if(check){
            //获得学生和教师双选全部信息
            SysTeaStu sys = new SysTeaStu();
            List<SysTeaStu> teaStus = sysTeaStuService.selectSysTeaStuList(sys);
            //获取所有学期的信息
            SysTerm sysTerm = new SysTerm();
            List<SysTerm> Terms = sysTermService.selectSysTermList(sysTerm);

            for (SysTeaStu sys1: teaStus //依次把所有学生的入学年份查出来
            ) {
                String stuid = sys1.getStuSno();
                String stu = stuid.substring(0,4);
                Long id = Long.parseLong(stuid);  //将学号转化为长整型
                String teaid = sys1.getTeaJno();
                Long id1 = Long.parseLong(teaid);  //将工号转化为长整型
                int year1 = Integer.parseInt(stu);
                Calendar c = Calendar.getInstance();  //获取系统当前年份
                int year = c.get(Calendar.YEAR) - 1;
                int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
                int count = 0; //当前学期数
                if(month<=7 && month>=1){
                    count = (year-year1)*2 + 1;
                }else {
                    count = (year-year1)*2;
                }
                count--;//第一个学期无课题
                count=3;
                if (count <= 5) {
                    SysTerm term = Terms.get(count); //查询出来当前学期的实践环节
                    SysUser teaUser = new SysUser(); //封装一个带有老师工号的对象
                    teaUser.setLoginName(teaid);
                    List<SysUser> teaUsers = sysUserService.selectUserList(teaUser); //反向查询该老师对象
                    SysUser stuUser = new SysUser(); //封装一个带有学生学号的对象
                    stuUser.setLoginName(stuid);
                    List<SysUser> stuUsers = sysUserService.selectUserList(stuUser); //反向查询该学生对象
                    String s1 = term.getTermTask1();
                    String s2 = term.getTermTask2();
                    String s3 = term.getTermTask3();
                    if (!s1.equals("无") ) {
                        SysTaskStu sysTaskStu = new SysTaskStu(); //插入的当前学期课题
                        SysUser user = stuUsers.get(0); //查询学生用户所有信息
                        SysUser user1 = teaUsers.get(0);  //查询教师用户所有信息
                        sysTaskStu.setStuId(id); //学生学号
                        sysTaskStu.setStuName(user.getUserName()); //学生姓名
                        sysTaskStu.setStuClass("RB软工" + stuid.substring(2, 4) + "级卓越"); //学生班级
                        sysTaskStu.setTaskClass(term.getTermTask1()); //当前实践环节名称
                        sysTaskStu.setStatus(0); //当前课题状态
                        sysTaskStu.setTeaId(id1); //当前老师学号
                        sysTaskStu.setTeaName(user1.getUserName()); //当前老师姓名
                        sysTaskStu.setScoreStatus(0);
                        sysTaskStu.setScore(0);  //将成绩统一设置为0
                        year = c.get(Calendar.YEAR);
                        if (month >= 1 && month <= 6) {
                            sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                        } else {
                            sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                        }
                        sysTaskStuService.insertSysTaskStu(sysTaskStu); //将数据插入表中
                    }
                    if (!s2.equals("无")) {
                        SysTaskStu sysTaskStu = new SysTaskStu(); //插入的当前学期课题
                        SysUser user = stuUsers.get(0); //查询学生用户所有信息
                        SysUser user1 = teaUsers.get(0);  //查询教师用户所有信息
                        sysTaskStu.setStuId(id); //学生学号
                        sysTaskStu.setStuName(user.getUserName()); //学生姓名
                        sysTaskStu.setStuClass("RB软工" + stuid.substring(2, 4) + "级卓越"); //学生班级
                        sysTaskStu.setTaskClass(term.getTermTask2()); //当前实践环节名称
                        sysTaskStu.setStatus(0); //当前课题状态
                        sysTaskStu.setTeaId(id1); //当前老师学号
                        sysTaskStu.setTeaName(user1.getUserName()); //当前老师姓名
                        sysTaskStu.setScoreStatus(0);
                        sysTaskStu.setScore(0);  //将成绩统一设置为0
                        year = c.get(Calendar.YEAR);
                        if (month >= 1 && month <= 6) {
                            sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                        } else {
                            sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                        }
                        sysTaskStuService.insertSysTaskStu(sysTaskStu); //将数据插入表中
                    }
                    if (!s3.equals("无")) {
                        SysTaskStu sysTaskStu = new SysTaskStu(); //插入的当前学期课题
                        SysUser user = stuUsers.get(0); //查询学生用户所有信息
                        SysUser user1 = teaUsers.get(0);  //查询教师用户所有信息
                        sysTaskStu.setStuId(id); //学生学号
                        sysTaskStu.setStuName(user.getUserName()); //学生姓名
                        sysTaskStu.setStuClass("RB软工" + stuid.substring(2, 4) + "级卓越"); //学生班级
                        sysTaskStu.setTaskClass(term.getTermTask3()); //当前实践环节名称
                        sysTaskStu.setStatus(0); //当前课题状态
                        sysTaskStu.setTeaId(id1); //当前老师学号
                        sysTaskStu.setTeaName(user1.getUserName()); //当前老师姓名
                        sysTaskStu.setScoreStatus(0);
                        sysTaskStu.setScore(0);  //将成绩统一设置为0
                        year = c.get(Calendar.YEAR);
                        if (month >= 1 && month <= 6) {
                            sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                        } else {
                            sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                        }
                        sysTaskStuService.insertSysTaskStu(sysTaskStu); //将数据插入表中
                    }
                }

            }
            return toAjax(true);
        }
        else {
            return toAjax(false);
        }
    }
}
